package com.lin.activity;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.lin.utils.Session;
import com.lin.utils.Session.ResultReceiver;
import com.lin.utils.Session.SignupResult;
import com.lin.views.ListViewForApplyFriends;
import com.lin.views.ListViewForFocus;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.BaseActivity;
import com.yunfeng.supercall.activity.ShareActivity;
import com.yunfeng.supercall.bean.Constants;
import com.yunfeng.supercall.bean.NewSuperFriendsBean;
import com.yunfeng.supercall.bean.SuperFriendsBean;
import com.yunfeng.supercall.bean.UserBean;
import com.yunfeng.supercall.httputils.HttpClient;
import com.yunfeng.supercall.httputils.MyAjaxCallBack;
import com.yunfeng.supercall.manager.PushManager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.Toast;

public class MyFriendsApplicationListActivity extends Activity implements ListViewForApplyFriends.OnItemListViewEventListener, OnClickListener, Constants{

	private ListViewForApplyFriends mListView;
	Session mSession;
	UserBean user;
	private FrameLayout mFrameLayoutMore;
	private FrameLayout mFrameLayoutBack;
	public HttpClient mHttpClient = new HttpClient();
	private SwipeRefreshLayout mSwipeRefreshLayout;

	ArrayList<NewSuperFriendsBean> mListData;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_application);

		user = BaseActivity.getUser(getApplicationContext());

		mSession = Session.getSession();
		mSession.init(getApplicationContext(), true);
		loadData();


		mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);

		mSwipeRefreshLayout.setRefreshing(true);
		mSwipeRefreshLayout.setColorSchemeResources(R.color.base_orange);

		mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				loadData();
			}
		});
		mListView = (ListViewForApplyFriends) findViewById(R.id.listview);
		mFrameLayoutMore = (FrameLayout) findViewById(R.id.framelayout_more);
		mFrameLayoutBack = (FrameLayout) findViewById(R.id.framelayout_back);

		mFrameLayoutBack.setOnClickListener(this);
		mFrameLayoutMore.setOnClickListener(this);
		mListView.setOnItemListViewEventListener(this);
	}

	private void loadData() {
		mSession.getFriendApyList(getApplicationContext(), user.getUserid(), 0, new ResultReceiver<ArrayList<NewSuperFriendsBean>>() {

			@Override
			public void onResult(boolean last, ArrayList<NewSuperFriendsBean> data) {
				// TODO Auto-generated method stub
				mSwipeRefreshLayout.setRefreshing(false);

				if(data == null) return;
				mListView.setDataList(data);
				mListData = data;
			}

		});
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if(mFrameLayoutBack == view) {
			finish();
		} else if(mFrameLayoutMore == view) {
			Intent intent = new Intent(this, ShareActivity.class);
			startActivity(intent);
		}
	}
	@Override
	public void onItemAvatarLisener(NewSuperFriendsBean maListInfo, int x, int y) {
		// TODO Auto-generated method stub

	}
	@Override
	public void onItemClickListener(NewSuperFriendsBean maListInfo, int x, int y) {
		// TODO Auto-generated method stub

	}
	@Override
	public void onItemStateListener(final NewSuperFriendsBean maListInfo, int x, int y) {
		// TODO Auto-generated method stub

		/*mSession.changeRelation(MyFriendsApplicationListActivity.this, maListInfo.getUserid(),  user.getUserid(),1, new ResultReceiver<SignupResult>() {

			@Override
			public void onResult(boolean last, SignupResult result) {
				// TODO Auto-generated method stub
				if(result == SignupResult.SUCCESS) {
					maListInfo.setState(1);
					mListView.setDataList(mListData);
					Toast.makeText(getApplicationContext(), MyFriendsApplicationListActivity.this.getResources().getString(R.string.add_success) + "2", Toast.LENGTH_SHORT).show();
				}
			}

		});
		 */
		/*	mSession.acceptFriendApy(MyFriendsApplicationListActivity.this,  user.getUserid(),maListInfo.getUserid(), 0,new ResultReceiver<SignupResult>() {

			@Override
			public void onResult(boolean last, SignupResult data) {
				// TODO Auto-generated method stub
				if(data == SignupResult.SUCCESS) {
						int position = mListData.indexOf(maListInfo);
					NewSuperFriendsBean mBean = mListData.get(position);
//					maListInfo.setState(1);
//					mListView.setDataList(mListData);
					mSession.getFriendApyList(getApplicationContext(), user.getUserid(), 1, new ResultReceiver<ArrayList<NewSuperFriendsBean>>() {

						@Override
						public void onResult(boolean last, ArrayList<NewSuperFriendsBean> data) {
							// TODO Auto-generated method stub
							mListView.setDataList(data);
							mListData = data;
						}

					});
					Toast.makeText(getApplicationContext(), MyFriendsApplicationListActivity.this.getResources().getString(R.string.add_success), Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getApplicationContext(), MyFriendsApplicationListActivity.this.getResources().getString(R.string.add_fail), Toast.LENGTH_SHORT).show();
				}
			}

		});*/


		JSONObject jsonObject = new JSONObject();
		int userid = user.getUserid();
		JSONObject json = BaseActivity.getBaseParams(getApplicationContext());

		try {
			json.put("i_apyid",  maListInfo.getId());
			json.put("i_state", 1);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.i("tag", json.toString());

		mHttpClient.post(FUNCTION_ACCEPT_FRIENDS, jsonObject, new MyAjaxCallBack() {

			@Override
			public void onReceiveError(String msg, String errorCode) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onReceiveData(String data, String msg) {
				// TODO Auto-generated method stub
				/*	int position = mListData.indexOf(maListInfo);
						NewSuperFriendsBean mBean = mListData.get(position);*/
				mSession.getFriendApyList(getApplicationContext(), user.getUserid(), 1, new ResultReceiver<ArrayList<NewSuperFriendsBean>>() {

					@Override
					public void onResult(boolean last, ArrayList<NewSuperFriendsBean> data) {
						// TODO Auto-generated method stub
						mListView.setDataList(data);
						mListData = data;
					}

				});
			}
		});
	}

}